#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=1e5+10;
int n,k,head[MAXN],cnt,nxt1[MAXN],nxt2[MAXN],mx,t,ans;
struct edge
{
	int v,next,val;
}e[MAXN*2];
void addedge(int x,int y)
{
	e[cnt]=(edge){y,head[x],1};
	head[x]=cnt++;
	return;
}
int dfs(int u,int fa)
{
	int mx1=0,mx2=0;
	for(int i=head[u];~i;i=e[i].next)
	{
		int v=e[i].v;
		if(v==fa)continue;
		int val=e[i].val+dfs(v,u);
		if(val>mx1)mx2=mx1,nxt2[u]=nxt1[u],mx1=val,nxt1[u]=i;
		else if(val>mx2)mx2=val,nxt2[u]=i;
	}
	if(mx1+mx2>mx)mx=mx1+mx2,t=u;
	return mx1;
}
int main()
{
	n=read();k=read();
	memset(head,-1,sizeof(head));
	memset(nxt1,-1,sizeof(nxt1));
	memset(nxt2,-1,sizeof(nxt2));
	for(int i=1;i<n;++i)
	{
		int x=read(),y=read();
		addedge(x,y);
		addedge(y,x);
	}
	ans=(n-1)<<1;
	dfs(1,0);
	ans-=mx-1;
	if(k==2)
	{
		for(int i=nxt1[t];~i;i=nxt1[e[i].v])
			e[i].val=e[i^1].val=-1;
		for(int i=nxt2[t];~i;i=nxt1[e[i].v])
			e[i].val=e[i^1].val=-1;
		mx=0;
		dfs(1,0);
		ans-=mx-1;
	}
	printf("%d\n",ans);
	return 0;
}
